﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Core.Views;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    public class ActionsPresenter
    {
        private IActionDAO _actionDAO;
        private List<IAction> _actions = new List<IAction>();
        private IAbstractDataView _view;

        public ActionsPresenter(IAbstractDataView view)
        {
            _actionDAO = ResolverContainer.Resolver.Resolve<IActionDAO>();
            view.AddRecord += AddRecord;
            view.Refreh += Refreh;
            view.DeleteRecord += DeletRecord;
            view.UpdateRecord += Update;
            _actions = _actionDAO.GetAll(false);
            view.Data.DataSource = _actions;
            view.Data.ResetBindings(true);
            _view = view;
            _view.ViewName = "Просмотр аукционов";
            view.Show();

        }

        private void AddRecord(object sender,EventArgs eventArgs )
        {
            var ap = new ActionPresenter(new ActionView());
            Refreh(this, null);
        }

        private void DeletRecord(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                _actionDAO.Delete((IDomainObject)current);
                Refreh(this, null);
            }
        }

        private void Refreh(object sender, EventArgs eventArgs )
        {
            _actions = _actionDAO.GetAll(false);
            _view.Data.DataSource = _actions;
            _view.Data.ResetBindings(true);
     
        }


        private void Update(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                var upd = new ActionPresenter(new ActionView(), (IAction) current);
                Refreh(this, null);
            }
        }
    }
}
